package com.simonall;

import com.simonall.conf.Propertys;
import com.simonall.listen.I18nListener;
import com.simonall.listen.PropertyListener;
import com.simonall.netty.Initializer;

import cn.hutool.core.collection.CollUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 主程序
 * 
 * @author simonall
 */
public class Main {

	// Listener Mount
	static {
		CollUtil.newLinkedList(new PropertyListener(), //
				new I18nListener()).forEach(listen -> listen.mount());
	}

	public static void main(String[] args) throws InterruptedException {
		if (Propertys.DEFAULT_SERVER_ENABLE) {
			EventLoopGroup bossGroup = new NioEventLoopGroup();
			EventLoopGroup workerGroup = new NioEventLoopGroup();
			try {
				ServerBootstrap bootstrap = new ServerBootstrap();
				bootstrap.group(bossGroup, workerGroup);
				bootstrap.channel(NioServerSocketChannel.class);
				bootstrap.childHandler(new Initializer());

				Integer port = Propertys.DEFAULT_SERVER_PORT;
				ChannelFuture future = bootstrap.bind(port).sync();
				if (future.isSuccess()) {
					// TODO 弹出启动提示
					future.channel().closeFuture().sync();
				}
			} finally {
				bossGroup.shutdownGracefully();
				workerGroup.shutdownGracefully();
			}
		}
	}
}
